<?php
/**
 * 牌局日志备份和缓存实日志
 * Created by PhpStorm.
 * Keter
 * 2016/8/1
 * @守护进程
 */
class PaijuDelHandler extends ScriptBase
{
    /**
     * 游戏服保存的牌局日志KEY
     */
    const LOG_NAME = 'cardLog';

    /**
     * 出错备份
     */
    const QUEEN_BACKUP_NAME = 'cardLogBackup';

    /**
     * 当前脚本暂停标志
     * 同类结果为TRUE时则暂停和停止，如1
     */
    const SUSPEND_TAG = 'PAIJU_LOG_HANDLER_SUSPEND'; //暂停

    /**
     * 停止脚本标志
     */
    const STOP_TAG = 'PAIJU_LOG_HANDLER_STOP'; //停止

    /**
     * PHP用牌局日志缓存KEY
     */
    const CACHE_KEY = 'PJL';

    /**
     * 读取数据处理条数  add by tmac
     */
    const CACHE_LIMIT_NUMBER = 1000;

    /**
     * 日志文件大小  add by tmac
     */
    const LOG_FILE_SIZE = 2540000;
    /**
     * 过期时间
     */
    private $_expireTime;

    /**
     * 牌局日志Redis
     */
    protected $paijuRedis;

    /**
     * 逻辑代码
     */
    public function logic()
    {
        set_time_limit(0);

        while (true) {
            try {
                $this->paijuRedis = $this->redis('paiju_log');

                $keys = $this->paijuRedis->keys('PJL:*');
                $num = count($keys) > 100 ? 100 : count($keys);
                $keysNum = array_slice($keys, 0, $num);
                if(count($keysNum) >1){
                    foreach($keysNum as $v){
                        $this->paijuRedis->del($v);
                    }
                }else{
                    sleep(10);
                }
                // 连接redis
                // 断开redis
                $this->deinitRedis('paiju_log');
                usleep(10000);
            } catch (Exception $e) {
                //--add by tmac 2016-12-20
                $this->deinitRedis('paiju_log');
                sleep(1);
                //--end add
                $this->e($e->getMessage());
            }
        }
    }

}
?>